﻿//======================================================================
//    This file is part of baijie_interface
//
//    Copyright (C) 2023 Shenzhen Baijie Technology Co., Ltd.All rights reserved.
//
//    filename : mainwindow.cpp
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//======================================================================

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <sys/stat.h>
#include <QApplication>
#include <QGraphicsDropShadowEffect>

static int g_screenWidth;
static int g_screenHeight;
static int g_firstShowFlag;   // 0:The MainWindow is displayed for the first time
static QScreen *g_screen;
static QPushButton *g_btnWifi;
static QPushButton *g_btnBluetooth;
static QPushButton *g_btnEthernet;
static QPushButton *g_btn4G;
static QPushButton *g_btnVoice;
static QPushButton *g_btnSerial;
static QPushButton *g_btnUdev;
static QPushButton *g_btnTouch;
static QPushButton *g_btnGpio;
static QPushButton *g_btnAllTest;
static QPushButton *g_btnSystem;
static QLabel *g_labelWifi;
static QLabel *g_labelBluetooth;
static QLabel *g_labelEthernet;
static QLabel *g_labelSim;
static QLabel *g_labelVoice;
static QLabel *g_labelSerial;
static QLabel *g_labelTouch;
static QLabel *g_labelGpio;
static QLabel *g_labelAllTest;
static QLabel *g_labelSystem;
static QLabel *g_labelUdev;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    g_screen = qApp->primaryScreen();                      // Width and height of thg_e g_screen
    g_screenWidth = g_screen->size().width();
    g_screenHeight = g_screen->size().height();

    QFont font;
    font.setLetterSpacing(QFont::PercentageSpacing,110);         // Word space
    font.setPointSize(15);
    ui->lbl_baijieTechnologyFunctionDisplay->setFont(font);
    setInterface();

    connect(g_btnWifi,SIGNAL(clicked(bool)),this,SLOT(btn_wifi_clicked()));
    connect(g_btnBluetooth,SIGNAL(clicked(bool)),this,SLOT(btn_bluetooth_clicked()));
    connect(g_btnEthernet,SIGNAL(clicked(bool)),this,SLOT(btn_ethernet_clicked()));
    connect(g_btn4G,SIGNAL(clicked(bool)),this,SLOT(btn_simModule_clicked()));
    connect(g_btnVoice,SIGNAL(clicked(bool)),this,SLOT(btn_voice_clicked()));
    connect(g_btnSerial,SIGNAL(clicked(bool)),this,SLOT(btn_serialPort_clicked()));
    connect(g_btnUdev,SIGNAL(clicked(bool)),this,SLOT(btn_udev_clicked()));
    connect(g_btnTouch,SIGNAL(clicked(bool)),this,SLOT(btn_touch_clicked()));
    connect(g_btnGpio,SIGNAL(clicked(bool)),this,SLOT(btn_gpio_clicked()));
    connect(g_btnAllTest,SIGNAL(clicked(bool)),this,SLOT(btn_allTest_clicked()));
    connect(g_btnSystem,SIGNAL(clicked(bool)),this,SLOT(btn_system_clicked()));

    connect(&g_voiceWg,SIGNAL(voice_back_msg()),this,SLOT(voice_back()));
    connect(&g_udevWg,SIGNAL(udev_back_msg()),this,SLOT(udev_back()));
    connect(&g_gpioWg,SIGNAL(gpio_back_msg()),this,SLOT(gpio_back()));
    connect(&g_touchWg,SIGNAL(touch_back_msg()),this,SLOT(touch_back()));
    connect(this,SIGNAL(wifi_status_msg()),&g_wifiWg,SLOT(wifi_status_show()));
    connect(&g_wifiWg,SIGNAL(wifi_back_msg()),this,SLOT(wifi_back()));
    connect(&g_ethernetWg,SIGNAL(ipset_back_msg()),this,SLOT(ethernet_back()));
    connect(&g_allWg,SIGNAL(all_test_back_msg()),this,SLOT(all_back()));
    connect(&g_bluetoothWg,SIGNAL(bluetooth_back_msg()),this,SLOT(bluetooth_back()));
    connect(&g_serialWg,SIGNAL(serial_back_msg()),this,SLOT(serial_back()));
    connect(&g_systemWg,SIGNAL(sys_back_msg()),this,SLOT(sys_back()));
    connect(&g_systemWg,SIGNAL(main_cn_msg()),this,SLOT(cn_main()));
    connect(&g_simModuleWg,SIGNAL(sim_back_msg()),this,SLOT(sim_module_back()));
    connect(this,SIGNAL(cn_msg()),this,SLOT(cn_main()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::btn_voice_clicked()
{
    this->hide();
    g_voiceWg.resize(g_screenWidth,g_screenHeight);
    g_voiceWg.show();
    g_voiceWg.activateWindow();
}

void MainWindow::btn_udev_clicked()
{
    this->hide();
    g_udevWg.resize(g_screenWidth,g_screenHeight);
    g_udevWg.show();
    g_udevWg.activateWindow();
}

void MainWindow::btn_gpio_clicked()
{
    this->hide();
    g_gpioWg.resize(g_screenWidth,g_screenHeight);
    g_gpioWg.show();
    g_gpioWg.activateWindow();
}

void MainWindow::btn_touch_clicked()
{
    this->hide();
    g_touchWg.resize(g_screenWidth,g_screenHeight);
    g_touchWg.show();
    g_touchWg.activateWindow();
}

void MainWindow::btn_wifi_clicked()
{
    this->hide();
    g_wifiWg.resize(g_screenWidth,g_screenHeight);
    g_wifiWg.show();
}
void MainWindow::btn_ethernet_clicked()
{
    this->hide();
    g_ethernetWg.resize(g_screenWidth,g_screenHeight);
    g_ethernetWg.show();
    g_ethernetWg.activateWindow();
}

void MainWindow::cn_main()                          // Chinese-english switch
{
    static bool languageFlag = 0;
    if(languageFlag)
    {
        qApp->removeTranslator(g_translator);
        delete g_translator;
        g_translator = NULL;
        g_database.updateTableOne("language","English");
    }
    else
    {
        g_translator = new QTranslator();
        g_translator->load(":/chinese");
        qApp->installTranslator(g_translator);
        g_database.updateTableOne("language","Chinese");
    }
    languageFlag = !languageFlag;
    ui->retranslateUi(this);
    g_labelBluetooth->setText(tr("Bluetooth"));
    g_labelEthernet->setText(tr("Ethernet"));
    g_labelSim->setText(tr("4G"));
    g_labelSerial->setText(tr("SerialPort"));
    g_labelTouch->setText(tr("Touch"));
    g_labelUdev->setText(tr("Storage"));
    g_labelVoice->setText(tr("Audio"));
    g_labelSystem->setText(tr("System"));
    g_labelAllTest->setText(tr("All Tests"));
    g_wifiWg.languageReload();
    g_ethernetWg.languageReload();
    g_gpioWg.languageReload();
    g_voiceWg.languageReload();
    g_udevWg.languageReload();
    g_bluetoothWg.languageReload();
    g_allWg.languageReload();
    g_serialWg.languageReload();
    g_simModuleWg.languageReload();
    g_systemWg.languageReload();
    emit wifi_status_msg();
}

void MainWindow::btn_bluetooth_clicked()
{
    this->close();
    g_bluetoothWg.resize(g_screenWidth,g_screenHeight);
    g_bluetoothWg.show();
    g_bluetoothWg.activateWindow();
}

void MainWindow::btn_allTest_clicked()
{
    this->hide();
    g_allWg.resize(g_screenWidth,g_screenHeight);
    g_allWg.show();
    g_allWg.activateWindow();
}

void MainWindow::btn_serialPort_clicked()
{
    this->hide();
    g_serialWg.resize(g_screenWidth,g_screenHeight);
    g_serialWg.show();
    g_serialWg.activateWindow();
}

void MainWindow::btn_system_clicked()
{
    this->hide();
    g_systemWg.resize(g_screenWidth,g_screenHeight);
    g_systemWg.show();
    g_systemWg.hide();
    g_systemWg.show();
    g_systemWg.activateWindow();
}

void MainWindow::btn_simModule_clicked()
{
    this->close();
    g_simModuleWg.resize(g_screenWidth,g_screenHeight);
    g_simModuleWg.show();
    g_simModuleWg.activateWindow();
}

void MainWindow::voice_back()
{
    g_voiceWg.close();
    this->show();
}

void MainWindow::udev_back()
{
    g_udevWg.hide();
    this->show();
    this->activateWindow();
}

void MainWindow::gpio_back()
{
    g_gpioWg.hide();
    this->show();
    this->activateWindow();
}

void MainWindow::touch_back()
{
    g_touchWg.hide();
    this->show();
    this->activateWindow();
}

void MainWindow::wifi_back()
{
    g_wifiWg.close();
    this->show();
    this->activateWindow();
}

void MainWindow::ethernet_back()
{
    g_ethernetWg.hide();
    this->show();
    this->activateWindow();
}

void MainWindow::all_back()
{
    g_allWg.hide();
    this->show();
    this->activateWindow();
}

void MainWindow::bluetooth_back()
{
    g_bluetoothWg.hide();
    this->show();
    this->activateWindow();
}

void MainWindow::serial_back()
{
    g_serialWg.hide();
    this->show();
    this->activateWindow();
}

void MainWindow::sim_module_back()
{
    g_simModuleWg.hide();
    this->show();
    this->activateWindow();
}

void MainWindow::sys_back()
{
    g_systemWg.close();
    this->show();
    this->activateWindow();
}

void MainWindow::showEvent(QShowEvent *event)
{
    if(g_firstShowFlag == 0)
    {
        QStringList list = g_database.tableShow("language");
        if(!list.isEmpty())
        {
            if(list.at(0) == "Chinese")
            {
                emit cn_msg();
            }
        }
        g_firstShowFlag++;
    }
    QWidget::showEvent(event);
}

void MainWindow::setInterface()
{
    this->setGeometry(0,0,g_screenWidth,g_screenHeight);
    int widgetHeight = g_screenHeight*5/6;
    int widgetWidth = g_screenWidth;
    g_btnWifi = new QPushButton(ui->widget);
    g_btnWifi->setStyleSheet("border-image: url(:/button_image/wifi.svg);"
                              "background-color:transparent;");
    g_btnWifi->setGeometry(widgetWidth*5/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*2/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnWifi->show();
    g_btnBluetooth = new QPushButton(ui->widget);
    g_btnBluetooth->setStyleSheet("border-image: url(:/button_image/bluetootht.svg);"
                                  "background-color:transparent;");
    g_btnBluetooth->setGeometry(widgetWidth*14/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*2/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnBluetooth->show();
    g_btnEthernet = new QPushButton(ui->widget);
    g_btnEthernet->setStyleSheet("border-image: url(:/button_image/ethernet.svg);"
                                 "background-color:transparent;");
    g_btnEthernet->setGeometry(widgetWidth*23/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*2/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnEthernet->show();
    g_btn4G = new QPushButton(ui->widget);
    g_btn4G->setStyleSheet("border-image: url(:/button_image/4g.svg);"
                           "background-color:transparent;");;
    g_btn4G->setGeometry(widgetWidth*32/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*2/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btn4G->show();

    g_btnVoice = new QPushButton(ui->widget);
    g_btnVoice->setStyleSheet("border-image: url(:/button_image/voice.svg);"
                              "background-color:transparent;");
    g_btnVoice->setGeometry(widgetWidth*5/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*14/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnVoice->show();
    g_btnSerial = new QPushButton(ui->widget);
    g_btnSerial->setStyleSheet("border-image: url(:/button_image/serial.svg);"
                               "background-color:transparent;");
    g_btnSerial->setGeometry(widgetWidth*14/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*14/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnSerial->show();
    g_btnUdev = new QPushButton(ui->widget);
    g_btnUdev->setStyleSheet(" border-image: url(:/button_image/udevt.svg);"
                             "background-color:transparent;");
    g_btnUdev->setGeometry(widgetWidth*23/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*14/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnUdev->show();
    g_btnTouch = new QPushButton(ui->widget);
    g_btnTouch->setStyleSheet("border-image: url(:/button_image/touch.svg);"
                              "background-color:transparent;");
    g_btnTouch->setGeometry(widgetWidth*32/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*14/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnTouch->show();

    g_btnGpio = new QPushButton(ui->widget);
    g_btnGpio->setStyleSheet("border-image: url(:/button_image/gpiot.svg);"
                             "background-color:transparent;");
    g_btnGpio->setGeometry(widgetWidth*5/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*26/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnGpio->show();
    g_btnAllTest = new QPushButton(ui->widget);
    g_btnAllTest->setStyleSheet("border-image: url(:/button_image/test_port.svg);"
                                "background-color:transparent;");
    g_btnAllTest->setGeometry(widgetWidth*14/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*26/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnAllTest->show();
    g_btnSystem = new QPushButton(ui->widget);
    g_btnSystem->setStyleSheet("border-image: url(:/button_image/system.svg);"
                               "background-color:transparent;");
    g_btnSystem->setGeometry(widgetWidth*23/45+(widgetWidth*8/45-widgetHeight*7/38)/2,widgetHeight*26/38,widgetHeight*7/38,widgetHeight*7/38);
    g_btnSystem->show();

    g_labelWifi = new QLabel(ui->widget);
    g_labelWifi->setAlignment(Qt::AlignCenter);
    g_labelWifi->setText(tr("WiFi"));
    g_labelWifi->setGeometry(widgetWidth*5/45,widgetHeight*19/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelWifi->show();
    g_labelBluetooth = new QLabel(ui->widget);
    g_labelBluetooth->setText(tr("Bluetooth"));
    g_labelBluetooth->setAlignment(Qt::AlignCenter);
    g_labelBluetooth->setGeometry(widgetWidth*14/45,widgetHeight*19/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelBluetooth->show();
    g_labelEthernet = new QLabel(ui->widget);
    g_labelEthernet->setAlignment(Qt::AlignCenter);
    g_labelEthernet->setText(tr("Ethernet"));
    g_labelEthernet->setGeometry(widgetWidth*23/45,widgetHeight*19/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelEthernet->show();
    g_labelSim = new QLabel(ui->widget);
    g_labelSim->setAlignment(Qt::AlignCenter);
    g_labelSim->setText(tr("4G"));
    g_labelSim->setGeometry(widgetWidth*32/45,widgetHeight*19/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelSim->show();

    g_labelVoice = new QLabel(ui->widget);
    g_labelVoice->setAlignment(Qt::AlignCenter);
    g_labelVoice->setText(tr("Audio"));
    g_labelVoice->setGeometry(widgetWidth*5/45,widgetHeight*43/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelVoice->show();
    g_labelSerial = new QLabel(ui->widget);
    g_labelSerial->setAlignment(Qt::AlignCenter);
    g_labelSerial->setText(tr("SerialPort"));
    g_labelSerial->setGeometry(widgetWidth*14/45,widgetHeight*43/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelSerial->show();
    g_labelUdev = new QLabel(ui->widget);
    g_labelUdev->setAlignment(Qt::AlignCenter);
    g_labelUdev->setText(tr("Storage"));
    g_labelUdev->setGeometry(widgetWidth*23/45,widgetHeight*43/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelUdev->show();
    g_labelTouch = new QLabel(ui->widget);
    g_labelTouch->setAlignment(Qt::AlignCenter);
    g_labelTouch->setText(tr("Touch"));
    g_labelTouch->setGeometry(widgetWidth*32/45,widgetHeight*43/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelTouch->show();

    g_labelGpio = new QLabel(ui->widget);
    g_labelGpio->setAlignment(Qt::AlignCenter);
    g_labelGpio->setText(tr("GPIO"));
    g_labelGpio->setGeometry(widgetWidth*5/45,widgetHeight*67/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelGpio->show();
    g_labelAllTest = new QLabel(ui->widget);
    g_labelAllTest->setAlignment(Qt::AlignCenter);
    g_labelAllTest->setText(tr("All Tests"));
    g_labelAllTest->setGeometry(widgetWidth*14/45,widgetHeight*67/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelAllTest->show();
    g_labelSystem = new QLabel(ui->widget);
    g_labelSystem->setAlignment(Qt::AlignCenter);
    g_labelSystem->setText(tr("System"));
    g_labelSystem->setGeometry(widgetWidth*23/45,widgetHeight*67/76,widgetWidth*8/45,widgetHeight*6/76);
    g_labelSystem->show();

    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(4, 4);
    shadowEffect->setColor(QColor (136, 138, 133));
    shadowEffect->setBlurRadius(5);
    ui->lbl_baijieTechnologyFunctionDisplay->setGraphicsEffect(shadowEffect);      // Add shadow to font

    qreal realX = g_screen->physicalDotsPerInchX();
    qreal realWidth = g_screenWidth / realX * 2.54;
    QFont font;

    if(realWidth < 15)
    {
        font.setPointSize(13);
        ui->lbl_baijieTechnologyFunctionDisplay->setFont(font);
        font.setPointSize(12);
    }
    else if (realWidth < 18)
    {
        font.setPointSize(16);
        ui->lbl_baijieTechnologyFunctionDisplay->setFont(font);
        font.setPointSize(15);
    }
    else
    {
        font.setPointSize(19);
        ui->lbl_baijieTechnologyFunctionDisplay->setFont(font);
        font.setPointSize(18);
    }

    g_labelWifi->setFont(font);
    g_labelBluetooth->setFont(font);
    g_labelEthernet->setFont(font);
    g_labelSim->setFont(font);
    g_labelVoice->setFont(font);
    g_labelSerial->setFont(font);
    g_labelTouch->setFont(font);
    g_labelGpio->setFont(font);
    g_labelAllTest->setFont(font);
    g_labelSystem->setFont(font);
    g_labelUdev->setFont(font);
}
